const pool = require('../tools/getPool');
const sendResponse = require('../tools/sendResponse');

module.exports = function(req, res){

	let account = req.body.account;
	let name = req.body.name;
	let department = req.body.department;
	let password = req.body.password;
	let type = req.body.type;


	if (account == undefined || name == undefined || department == undefined  || type == undefined || password == undefined) {
		sendResponse('error','参数缺少', null, res);
		return;
	}

	pool.getConnection(function(err, connection) {
		if (err) { 
			sendResponse('error','database error', null, res);
			return;
		}
		let params = [account, password, name, department, type];
		connection.query('INSERT INTO clerk (account, password, name, department, type) VALUES (?,?,?,?,?);', params, function (error, rows, fields) {

			connection.release();

			if (error) {
				if (error.code = "ER_DUP_ENTRY") {
					console.log(error)
					sendResponse('error','账户重复，请检查', null, res);
					return;
				} else {
					sendResponse('error','数据库错误', null, res);
					return;
				}
			} else {
				sendResponse('ok','添加成功', null, res);
				return;
			}

		})	

	});

}